home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <Resources.h>
- #include <Memory.h>
- #include <CursorCtl.h>
- #include <ErrMgr.h>
- #include <TextUtils.h>
-
- #include "pap.h" // from PAPWorkStation.o package
- #include "CurrentPrinter.h" // from the CurrentPrinter sample
-
- ////////////////////////////////////////////////////////////////////////////////////
- // functions to bridge from old names to new
- ////////////////////////////////////////////////////////////////////////////////////
- short PAPRead(short refNum,Ptr ReadBuff,short *DataSize,short *eof,short *CompState)
- {
- return WPAPRead(refNum, (void *)ReadBuff, DataSize, eof,(OSErr *)CompState);
- }
-
- short PAPWrite(short refNum,Ptr WriteBuff,short DataSize,short eof,short *CompState)
- {
- return WPAPWrite(refNum,(void *)WriteBuff, DataSize, eof,(OSErr *)CompState);
- }
-
- short PAPClose(short refNum)
- {
- return WPAPClose(refNum);
- }
-
- static void ConvertEOLs(char *buffer,short length)
- {
- short i;
- if (length<0) {
- DebugStr("\pFoolish Human! Bad length");
- return;
- }
- if (!buffer) {
- DebugStr("\pFoolish Human! Bad buffer");
- return;
- }
- for(i=0;i<length;i++) {
- if (buffer[i] == '\012')
- buffer[i] = '\015';
- }
- }
-
- void displayStatus (struct PAPStatusRec *status)
- {
- static Str255 oldStatus;
-
- if (EqualString(oldStatus,status->statusStr,false,false))
- // nothing has changed - just return
- return;
- else {
- // copy in the new status
- BlockMove(status->statusStr,oldStatus,(status->statusStr[0])+1);
- // and display it
- // this assumes a printf that knows about pascal strings. If you
- // don't have one, you'll need to convert the string yourself.
- fprintf(stderr,"### %p\n",oldStatus);
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
- // here's where we do all the work
- ////////////////////////////////////////////////////////////////////////////////////
- int main(int argc, char **argv)
- {
- Str255 lwname;
- int fd, i;
- char wbuffer[4096], rbuffer[4096];
- struct PAPStatusRec status;
- AddrBlock printerAddress;
- short rcnt, wcnt, reof;
- short refNum, wstate, rstate;
- char msg[256];
-
- if (argc != 2) {
- fprintf(stderr, "### usage: %s filename\n", argv[0]);
- exit(1);
- }
- fd = open(argv[1], 0);
- if (fd < 0) {
- fprintf(stderr, "### %s: can't open '%s' for reading\n", argv[0], argv[1]);
- exit(1);
- }
-
- if (GetCurrentPrinter(lwname) != noErr) exit(1);
-
- InitCursorCtl(NULL);
- Show_Cursor(WATCH_CURSOR);
-
- i = PAPOpen(&refNum, (EntityName *) lwname, 8,&status, &wstate);
- if (i) {
- fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(i, msg));
- exit(1);
- }
- while (wstate > 0) {
- // spin the cursor (which lets MPW check for cancels for us)
- RotateCursor(32);
- // oh, and display some status, too....
- displayStatus(&status);
- }
- if (wstate) {
- fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(wstate, msg));
- PAPClose(refNum);
- WPAPUnload();
- exit(1);
- }
- // DebugStr("\pOpened");
-
- // Issue a PAPRead so when the printer wants to talk to us, it can
- rcnt = sizeof(rbuffer);
- reof = 0;
- PAPRead(refNum, (Ptr) rbuffer, &rcnt, &reof, &rstate);
- // DebugStr("\pIssued that first read...");
-
- while ((i = read(fd, (Ptr) wbuffer, sizeof(wbuffer))) != 0) {
- wcnt = i;
- RotateCursor(32);
- PAPWrite(refNum, (Ptr) wbuffer, wcnt, 0, &wstate);
- while (wstate > 0) {
- if (rstate == 0) {
- for (i = 0; i < rcnt; i++)
- if (rbuffer[i] != '\012') putchar(rbuffer[i]);
- fflush(stdout);
- rcnt = sizeof(rbuffer);
- reof = 0;
- RotateCursor(-32);
- PAPRead(refNum, (Ptr) rbuffer, &rcnt, &reof, &rstate);
- } else if (rstate < 0) {
- fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(rstate, msg));
- PAPClose(refNum);
- WPAPUnload();
- exit(1);
- } else {
- // We're not doing anything else, check the status
- PAPStatus((EntityName *) lwname,&status,&printerAddress);
- displayStatus(&status);
- }
- }
- if (wstate < 0) {
- fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(wstate, msg));
- PAPClose(refNum);
- WPAPUnload();
- exit(1);
- }
- // DebugStr("\pSent a buffer");
- }
- RotateCursor(32);
- PAPWrite(refNum, (Ptr) "", 0, 1, &wstate);
- // DebugStr("\pSent EOF");
- for(;;) { // we check reof and break to exit
- if (rstate == 0) {
- char foo[4096];
-
- memset(foo,0,4096);
- strncpy(foo,rbuffer,rcnt);
- ConvertEOLs(foo,rcnt);
- printf("%s",foo);
- fflush(stdout);
- if (reof)
- // we received the end of file from the printer. We're done.
- break;
- rcnt = sizeof(rbuffer);
- reof = 0;
- RotateCursor(-32);
- PAPRead(refNum, (Ptr) rbuffer, &rcnt, &reof, &rstate);
- } else if (rstate < 0) {
- // got an error
- fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(rstate, msg));
- PAPClose(refNum);
- WPAPUnload();
- exit(1);
- } else {
- // We're not done yet, so check the status
- PAPStatus((EntityName *) lwname,&status,&printerAddress);
- displayStatus(&status);
- }
- }
- PAPClose(refNum);
- WPAPUnload();
- // DebugStr("\pClosed normally");
- }
-